#include <iostream>
#include <string>
#include <cassert>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "Task.hpp"

using std::cout;
using std::endl;
using std::vector;
using std::string;
using std::pair;
using std::make_pair;

#define PROCESS_NUM 5

int waitCommand(int waitfd, bool& quit)  // 等待对方发送命令
{
    uint32_t command = 0;
    ssize_t s = read(waitfd, &command, sizeof(command));
    if (s == 0)
    {
        quit = true;
        return -1;
    }
    assert(s == sizeof(uint32_t));
    return command;
}

void sendAndWakeUp(pid_t who, int fd, uint32_t command)
{
    write(fd, &command, sizeof(command));
    cout << "main process: call process: " << who << " execute " << desc[command] << "throuht: " << fd << endl;
}

int main()
{
    load();
    vector<pair<pid_t, int>> slots;
    
    // 创建多个进程
    for (int i = 0; i < PROCESS_NUM; i++)
    {
        // 创建管道
        int pipefd[2] = {0};
        int n = pipe(pipefd);
        assert(n == 0);
        (void) n;

        pid_t id = fork();
        assert(id != -1);
        
        if (id == 0)
        {
            // 子进程
            close(pipefd[1]); // 关闭写端
            while (true)
            {
                // 等待命令
                bool quit = false;
                int command = waitCommand(pipefd[0], quit);  // 如果对方不发，我们就阻塞
                if (quit) break; // true 代表读完了
                if (command >= 0 & command < HandlerSize())
                {
                    callback[command]();
                }
                else
                {
                    cout << "非法的command: " << command << endl;
                }
            }

            exit(1);
        }

        // 父进程进行写入
        close(pipefd[0]); // 关闭读端
        slots.push_back(pair<pid_t, int>(id, pipefd[1]));
    }

    // 父进程派发任务
    srand((unsigned long)time(nullptr) ^ getpid() ^ 22233311231);  // 让数据更随机一些
    while (true)
    {
        // int select;
        // int command;
        // cout << "#############################################" << endl;
        // cout << "##### 1. show funcitons   2.send command ####" << endl;
        // cout << "#############################################" << endl;
        // cout << "Please Select: ";
        // std::cin >> select;
        // if (select == 1)
        //     Showhandler();
        // else if (select == 2)
        // {
        //     cout << "Enter Your Command# ";
        //     std::cin >> command;

        //     // 随机选择进程
        //     int choice = rand() % callback.size();

        //     // 把任务发送给进程
        //     sendAndWakeUp(slots[choice].first, slots[choice].second, command);
        // }
        // else
        // {
        //     cout << "选择错误!" << endl;
        //     continue;
        // }


        // 让它自己跑
        int command = rand() % HandlerSize(); // 选择一个任务
        int choice = rand() % HandlerSize();  // 选择一个进程
        sendAndWakeUp(slots[choice].first, slots[choice].second, command);
        sleep(1);

    }

    // 关闭fd, 结束所有的子进程
    for (const auto& e : slots)
    {
        close(e.second);
    }

    // 回收子进程
    for (const auto& e : slots)
    {
        waitpid(e.first, nullptr, 0);
    }
    return 0;
}